#include<stddef.h>

void *binsearch(const void *key, const void *base, int nmemb, int size, int (*compar)(const void *, const void *)){
 	int left = 0;
	int right = nmemb - 1;
	int mid;
	while(left <= right){
		mid = left + (right - left) / 2;
		if(compar((char *)base + mid * size, key) == 0){
			break;
		}else if(compar((char *)base + mid * size, key) > 0){
			right = mid - 1;
		}else if(compar((char *)base + mid * size, key) < 0){
			left = mid + 1;
		}
	}
	if(left <= right){
		return (void *)base + mid * size;
	}else{
		return NULL;
	}
 
 }

